#include <iostream>
using namespace std;

/*用户表接口*/
class IUser
{
public:
    virtual void Insert()  = 0;
    virtual void GetUser() = 0;
};

/* SqlServer数据库访问User表*/
class SqlServerUser : public IUser
{
public:
    void Insert()
    {
        cout<<"在SQL Server中给User表增加一条记录"<<endl;
    }
    void GetUser()
    {
        cout<<"在SQL Server中给User表获取一条记录"<<endl;
    }
};

/* Access数据库访问User表 */
class AccessUser : public IUser
{
public:
    void Insert()
    {
        cout<<"在Access中给User表增加一条记录"<<endl;
    }
    void GetUser()
    {
        cout<<"在Access中User表获取一条记录"<<endl;
    }
};

/* Department表接口 */
class IDepartment
{
public:
    virtual void Insert() = 0;
    virtual void GetDepartment() = 0;
};

/* SqlServer数据库访问Department表*/
class SqlServerDepartment : public IDepartment
{
public:
    void Insert()
    {
        cout<<"在SQL Server中给Department表增加一条记录"<<endl;
    }
    void GetDepartment()
    {
        cout<<"在SQL Server中Department获取一条记录"<<endl;
    };
};

/* Access数据库访问Department表 */
class AccessDepartment : public IDepartment
{
public:
    void Insert()
    {
        cout<<"在Access中给Department表增加一条记录"<<endl;
    }
    void GetDepartment()
    {
        cout<<"在Access中Department获取一条记录"<<endl;
    };
};

/* 抽象工厂接口*/
class IFactory
{
public:
    virtual IUser* CreateUser() = 0;
    virtual IDepartment* CreateDepartment() = 0;
};

/* SqlServer工厂实现*/
class SqlServerFactory : public IFactory
{
    IUser* CreateUser()
    {
        return new SqlServerUser();
    }
    IDepartment* CreateDepartment()
    {
        return new SqlServerDepartment();
    }
};

/* Access工厂实现 */
class AccessFactory : public IFactory
{
    IUser* CreateUser()
    {
        return new AccessUser();
    }
    IDepartment* CreateDepartment()
    {
        return new AccessDepartment();
    }
};

/* 客户代码 */
int main()
{
    //创建工厂
    IFactory * pFactory = NULL;
    IUser * pUser = NULL;
    IDepartment * pDepartment = NULL;
    int choise;
    cout<<"选择数据库: ";
    cin>>choise;
    switch(choise)
    {
    case 1:
        pFactory= new SqlServerFactory(); //创建SqlServer访问的工厂
        break;
    case 2:
        pFactory = new AccessFactory(); //创建Access访问的工厂
        break;
    }
    //一致的操作
    pUser = pFactory->CreateUser();
    pDepartment= pFactory->CreateDepartment();
    pUser->Insert();
    pUser->GetUser();
    pDepartment->Insert();
    pDepartment->GetDepartment();

    return 0;
}
